# encoding: utf-8
# @author: w4dll
# @file: main.py
# @time: 2020/4/19 17:27

from login_pane import LoginPane
from register_pane import RegisterPane
from main_pane import MainPane
from mytool import *

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    # 用户界面创建
    main_pane = MainPane()                      # 主界面
    login_pane = LoginPane()                    # 登录界面

    register_pane = RegisterPane(login_pane)    # 注册界面
    register_pane.move(0, login_pane.height())  # 开始移动到最下方
    register_pane.show()

    db = sqlite3Class("./data.db")    # 数据库连接类

    # 注册按钮，显示注册界面动画
    def show_register_pane():
        login_pane.setWindowTitle("注册")    # 修改窗体标题
        show_out_bounce_animation(register_pane, b"pos", QPoint(0, login_pane.height()), QPoint(0, 0), 1000)  # 显示注册界面
        register_pane.show_lever1_combox()  # 每次显示注册界面重载一级下拉框

    # 注册界面-退出界面动画
    def exit_register_pane():
        login_pane.setWindowTitle("登录")  # 退出时，将登录界面修改回来

        # 退出注册界面动画
        show_out_bounce_animation(register_pane, b"pos", QPoint(0, 0), QPoint(login_pane.width(), 0), 1000)

        # 清空当前界面控件内容
        for child in register_pane.widget.children():
            if child.inherits("QLineEdit") or child.inherits("QComboBox"):
                child.clear()

        # # 刷新单位信息
        login_pane.comboBox.clear()
        usr_info = db.selectdata("select distinct 用户名 from t_userinfo")
        i = 0
        for x in usr_info:
            login_pane.comboBox.addItem(x[0])
            login_pane.comboBox.setItemIcon(i, QIcon(":/login/images/login3.png"))
            i += 1

    # 注册界面-点击注册按钮，发送退出信号
    def register_user(list_userinfo):
        sql = "select * from t_userinfo where 用户名='"+list_userinfo[0]+"'"
        userinfo = db.selectdata(sql)
        if len(userinfo) > 0:
            # 重名，重置用户用户名和密码
            register_pane.acount_le.clear()
            register_pane.acount_le.setPlaceholderText("用户名已存在！")
            register_pane.password_le.clear()
            register_pane.confirm_pwd_le.clear()
            register_pane.show_error_animation()
            return
        else:   # 用户不存在,可以注册 todo 用户密码加密的问题
            sql = "insert into t_userinfo(用户名,密码,姓名,性别,一级单位,二级单位,三级单位,注册时间,用户权限) values(?,?,?,?,?,?,?,?,?)"
            val = list_userinfo
            if list_userinfo[0] == "dll":
                 val += (10,)
            else:
                val += (1,)
            db.insert_singlerow_data(sql, val)

            QMessageBox.about(register_pane, "注册", "恭喜你，注册成功！")
            exit_register_pane()

    # todo 登录按钮
    def user_login(account, password):
        sql = "select 密码 from t_userinfo where 用户名='" + account + "'"
        pwd = db.selectdata(sql)
        if len(pwd) > 0 and pwd[0][0] == password:
            s = [account, password, str(login_pane.checkBox.isChecked()), str(login_pane.checkBox_2.isChecked())]
            sql = "update t_setting set loginstate='{}'".format(",".join(s))
            db.excuteSql(sql)

            # 设置用户名，显示主界面，隐藏登录界面
            main_pane.label_currentUser.setText(account)  # 要设置在显示前边，不然显示事件报错
            main_pane.show()
            login_pane.hide()
        else:
            login_pane.show_error_animation()
            login_pane.lineEdit.clear()

    # 信号链接
    login_pane.show_register_signal.connect(show_register_pane)     # 点击注册按钮时，发送信号外部处理
    register_pane.exit_singal.connect(exit_register_pane)
    register_pane.register_singal.connect(register_user)
    login_pane.login_signal.connect(user_login)

    login_pane.show()
    sys.exit(app.exec_())